$(function () {
  var apiBaseUrl = 'http://218.205.115.243:18080/afot-web/';
  var apiUrl = apiBaseUrl + 'submitSurvey';
  var sending = false;

  $('.img-box').on('click', '.icon-star', function () {
    var $this = $(this);
    var index = $this.index();
    var fillClassName = 'fill';
    if ($this.hasClass(fillClassName)) {
      $this.nextAll().removeClass(fillClassName);
    } else {
      $this.addClass(fillClassName)
      $this.prevAll().addClass(fillClassName);
    }
  });

  $('.submit-button').on('click', function() {
    if (sending) return;
    submit();
  });

  function getPostData() {
    var data = {
      userId: 1,
      surveyScenic: 1
    };
    $('.img-box').each(function(key, item) {
      var $el = $(item);
      data[$el.data('param')] = $el.children('.fill').length * 2;
    })
    data['surveyComments'] = $('textarea[name="surveyComments"]').val();
    return data;
  }

  function submit() {
    var data = getPostData();
    sending = true;
    $.ajax({
      type: 'POST',
      url: apiUrl,
      data: data,
      contentType: 'application/json; charset=utf-8',
      dataType: 'json',
      success: function(ret) {
        sending = false
        if (ret.operStatus == 1) {
          alert('提交成功');
          setTimeout(function() {
            window.close();
          }, 3000);
        } else {
          alert(ret.operMsg)
        }
      },
      error: function(err) {
        sending = false
        alert('提交失败，请重试')
      }
    })
  }
});
